﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using C4F.DevKit.Contacts;
using System.IO;
using C4F.DevKit.Contacts.Data;

namespace C4F.DevKit.Contacts
{
    /// <summary>
    /// This class represents a lightweight Contact object. Each property on this class is mapped to a property in the Contact object.
    /// </summary>
    /// <remarks>This object uses a Contact object as its source. They are kepy in synch when retrieving and persisting properties. Each property on this object is bound to a property on the Contact object source.</remarks>
    public class SimpleContactView
        : ContactView
    {

        public SimpleContactView(Contact source)
            : base(source)
        {
            ThumbnailSize = new System.Drawing.Size(60, 50);
        }

        private string _newPicFilePath = string.Empty;

        #region Properties
        
        /// <summary>
        /// The id of the contact
        /// </summary>
        public string ContactID
        {
            get
            {
                return base.Source.Id;
            }
        }
        
        /// <summary>
        /// The type of contact
        /// </summary>
        public ContactType ContactType
        {
            get
            {
                return base.Source.ContactType;
            }
        }

        /// <summary>
        /// The picture associated with the contact as a System.Drawing.Image object
        /// </summary>
        public System.Drawing.Image PicAsImage
        {
            get
            {
                if (base.Source.Photos[PhotoLabels.UserTile].Value != null)
                    return System.Drawing.Image.FromStream(base.Source.Photos[PhotoLabels.UserTile].Value);
                else 
                    return null;
            }            
        }

        /// <summary>
        /// The picture associated with the contact as a System.Drawing.Image object
        /// </summary>
        public System.Windows.Media.Imaging.BitmapSource PicAsBitmapSource
        {
            get
            {
                if (base.Source.Photos[PhotoLabels.UserTile].Value != null)
                    return UserTile.GetFramedPhoto(base.Source.Photos[PhotoLabels.UserTile], 96);
                else
                    return null;
            }
        }

        /// <summary>
        /// The thumbnail version of the picture associated with the contact.
        /// </summary>
        public System.Drawing.Image ThumbnailPic
        {
            get
            {
                if (PicAsImage != null)
                    return PicAsImage.GetThumbnailImage(ThumbnailSize.Width, ThumbnailSize.Height, null, IntPtr.Zero);
                else
                    return null;
            }
        }

        /// <summary>
        /// The size of the picture thumbnail.
        /// </summary>
        public System.Drawing.Size ThumbnailSize { get; set; }

        /// <summary>
        /// The file path used when saving a new to the contact. Once the save operation completes, the pic properties will be populate with the actual image.
        /// </summary>
        public string NewPicFilePath {
            set
            {
                if (!string.IsNullOrEmpty(value))
                {
                    PhotoBuilder pb = new PhotoBuilder();
                    using (FileStream fs = new FileStream(value, FileMode.Open, FileAccess.Read, System.IO.FileShare.ReadWrite))
                    {
                        pb.Value = fs;
                        pb.ValueType = "image";
                        base.Source.Photos[PhotoLabels.UserTile] = pb.ToPhoto();
                    }
                }
                else
                {
                    Stream disposable = base.Source.Photos[PhotoLabels.UserTile].Value;
                    C4F.DevKit.Contacts.Standard.Utility.SafeDispose(ref disposable);
                }
            }
        }

        /// <summary>
        /// The first name (given name) of the contact.
        /// </summary>
        public string FirstName
        {
            get
            {
                return base.Source.Names.Default.GivenName;
            }
            set
            {                
                NameBuilder name = new NameBuilder(base.Source.Names.Default);
                name.GivenName = value;
                base.Source.Names.Default = name.ToName();

                UpdateFormattedName();
            }
        }

        /// <summary>
        /// The last name (family name) of the contact.
        /// </summary>
        public string LastName 
        {
            get
            {
                return base.Source.Names.Default.FamilyName;
            }
            set
            {                
                NameBuilder name = new NameBuilder(base.Source.Names.Default);
                name.FamilyName = value;
                base.Source.Names.Default = name.ToName();

                UpdateFormattedName();
            }
        }

        /// <summary>
        /// The middle name of the contact.
        /// </summary>
        public string MiddleName
        {
            get
            {
                return base.Source.Names.Default.MiddleName;
            }
            set
            {
                NameBuilder name = new NameBuilder(base.Source.Names.Default);
                name.MiddleName = value;
                base.Source.Names.Default = name.ToName();

                UpdateFormattedName();
            }
        }

        /// <summary>
        /// The suffix of the contact (i.e. Jr, Sr, Ph.D., D.D.S.).
        /// </summary>
        public string Suffix
        {
            get
            {
                return base.Source.Names.Default.Suffix;
            }
            set
            {                
                NameBuilder name = new NameBuilder(base.Source.Names.Default);
                name.Suffix = value;
                base.Source.Names.Default = name.ToName();
            }
        }

        /// <summary>
        /// The prefix of the contact.
        /// </summary>
        public string Prefix
        {
            get
            {
                return base.Source.Names.Default.Prefix;
            }
            set
            {                
                NameBuilder name = new NameBuilder(base.Source.Names.Default);
                name.Prefix = value;
                base.Source.Names.Default = name.ToName();
            }
        }

        /// <summary>
        /// The formatted name of the contact.
        /// </summary>
        /// <remarks>
        /// The name is formatted as: First Middle Last.
        /// 
        /// This property may not be set.
        /// </remarks>
        public string FormattedName
        {
            get
            {
                return base.Source.Names.Default.FormattedName;
            }
            set
            {
                UpdateFormattedName();
            }
        }

        /// <summary>
        /// The title of the contact.
        /// </summary>
        public string Title
        {
            get
            {
                return base.Source.Names.Default.PersonalTitle;
            }
            set
            {                
                NameBuilder name = new NameBuilder(base.Source.Names.Default);
                name.PersonalTitle = value;
                base.Source.Names.Default = name.ToName();
            }
        }

        /// <summary>
        /// The nickname of the contact.
        /// </summary>
        public string NickName
        {
            get
            {
                return base.Source.Names.Default.NickName;
            }
            set
            {                
                NameBuilder name = new NameBuilder(base.Source.Names.Default);
                name.NickName = value;
                base.Source.Names.Default = name.ToName();
            }
        }

        /// <summary>
        /// Notes for the contact.
        /// </summary>
        public string Notes
        {
            get
            {
                return base.Source.Notes;
            }
            set
            {
                base.Source.Notes = value;
            }
        }

        /// <summary>
        /// The gender of the contact.
        /// </summary>
        public Gender Gender
        {
            get
            {
                return base.Source.Gender;
            }
            set
            {
                base.Source.Gender = value;
            }
        }

        #region Address Properties

        /// <summary>
        /// The contact's home street address
        /// </summary>
        public string HomeStreet
        {
            get
            {
                return base.Source.Addresses[PropertyLabels.Personal].Street;
            }
            set
            {
                PhysicalAddressBuilder address = new PhysicalAddressBuilder(base.Source.Addresses[PropertyLabels.Personal]);
                address.Street = value;
                base.Source.Addresses[PropertyLabels.Personal] = address.ToPhysicalAddress();
            }
        }

        /// <summary>
        /// The contact's home city
        /// </summary>
        public string HomeCity
        {
            get
            {
                return base.Source.Addresses[PropertyLabels.Personal].City;
            }
            set
            {
                
                PhysicalAddressBuilder address = new PhysicalAddressBuilder(base.Source.Addresses[PropertyLabels.Personal]);
                address.City = value;
                base.Source.Addresses[PropertyLabels.Personal] = address.ToPhysicalAddress();
            }
        }

        /// <summary>
        /// The contact's home state
        /// </summary>
        public string HomeState
        {
            get
            {
                return base.Source.Addresses[PropertyLabels.Personal].State;
            }
            set
            { 
                PhysicalAddressBuilder address = new PhysicalAddressBuilder(base.Source.Addresses[PropertyLabels.Personal]);
                address.State = value;
                base.Source.Addresses[PropertyLabels.Personal] = address.ToPhysicalAddress();
            }
        }

        /// <summary>
        /// The contacts home country
        /// </summary>
        public string HomeCountry
        {
            get
            {
                return base.Source.Addresses[PropertyLabels.Personal].Country;
            }
            set
            {                
                PhysicalAddressBuilder address = new PhysicalAddressBuilder(base.Source.Addresses[PropertyLabels.Personal]);
                address.Country = value;
                base.Source.Addresses[PropertyLabels.Personal] = address.ToPhysicalAddress();
            }
        }

        /// <summary>
        /// The contact's home zip code
        /// </summary>
        public string HomeZip
        {
            get
            {
                return base.Source.Addresses[PropertyLabels.Personal].ZipCode;                
            }
            set
            {
                PhysicalAddressBuilder address = new PhysicalAddressBuilder(base.Source.Addresses[PropertyLabels.Personal]);
                address.ZipCode = value;
                base.Source.Addresses[PropertyLabels.Personal] = address.ToPhysicalAddress();
            }
        }

        /// <summary>
        /// The contact's business street address
        /// </summary>
        public string BusinessStreet
        {
            get
            {
                return base.Source.Addresses[PropertyLabels.Business].Street;
            }
            set
            {
                PhysicalAddressBuilder address = new PhysicalAddressBuilder(base.Source.Addresses[PropertyLabels.Business]);
                address.Street = value;
                base.Source.Addresses[PropertyLabels.Business] = address.ToPhysicalAddress();
            }
        }

        /// <summary>
        /// The contact's business city
        /// </summary>
        public string BusinessCity
        {
            get
            {
                return base.Source.Addresses[PropertyLabels.Business].City;
            }
            set
            {
                PhysicalAddressBuilder address = new PhysicalAddressBuilder(base.Source.Addresses[PropertyLabels.Business]);
                address.City = value;
                base.Source.Addresses[PropertyLabels.Business] = address.ToPhysicalAddress();
            }
        }

        /// <summary>
        /// The contact's buiness state
        /// </summary>
        public string BusinessState
        {
            get
            {
                return base.Source.Addresses[PropertyLabels.Business].State;
            }
            set
            {                
                PhysicalAddressBuilder address = new PhysicalAddressBuilder(base.Source.Addresses[PropertyLabels.Business]);
                address.State = value;
                base.Source.Addresses[PropertyLabels.Business] = address.ToPhysicalAddress();
            }
        }

        /// <summary>
        /// The contact's business country
        /// </summary>
        public string BusinessCountry
        {
            get
            {
                return base.Source.Addresses[PropertyLabels.Business].Country;
            }
            set
            {
                PhysicalAddressBuilder address = new PhysicalAddressBuilder(base.Source.Addresses[PropertyLabels.Business]);
                address.Country = value;
                base.Source.Addresses[PropertyLabels.Business] = address.ToPhysicalAddress();
            }
        }

        /// <summary>
        /// The contact's business zip code
        /// </summary>
        public string BusinessZip
        {
            get
            {
                return base.Source.Addresses[PropertyLabels.Business].ZipCode;
            }
            set
            {
                PhysicalAddressBuilder address = new PhysicalAddressBuilder(base.Source.Addresses[PropertyLabels.Business]);
                address.ZipCode = value;
                base.Source.Addresses[PropertyLabels.Business] = address.ToPhysicalAddress();
            }
        }

        #endregion Address Properties

        #region Phone Properties
        public string BusinessPhone
        {
            get
            {
                return base.Source.PhoneNumbers[PropertyLabels.Business, PhoneLabels.Voice].Number;
            }
            set
            {                
                PhoneNumberBuilder phoneNumber = new PhoneNumberBuilder(base.Source.PhoneNumbers[PropertyLabels.Business, PhoneLabels.Voice]);
                phoneNumber.Number = value;
                base.Source.PhoneNumbers[PropertyLabels.Business, PhoneLabels.Voice] = phoneNumber.ToPhoneNumber();
            }
        }

        public string BusinessMobile
        {
            get
            {
                return base.Source.PhoneNumbers[PropertyLabels.Business, PhoneLabels.Mobile].Number;
            }
            set
            {                
                PhoneNumberBuilder phoneNumber = new PhoneNumberBuilder(base.Source.PhoneNumbers[PropertyLabels.Business, PhoneLabels.Mobile]);
                phoneNumber.Number = value;
                base.Source.PhoneNumbers[PropertyLabels.Business, PhoneLabels.Mobile] = phoneNumber.ToPhoneNumber();
            }
        }

        public string BusinessFax
        {
            get
            {
                return base.Source.PhoneNumbers[PropertyLabels.Business, PhoneLabels.Fax].Number;
            }
            set
            {
                PhoneNumberBuilder phoneNumber = new PhoneNumberBuilder(base.Source.PhoneNumbers[PropertyLabels.Business, PhoneLabels.Fax]);
                phoneNumber.Number = value;
                base.Source.PhoneNumbers[PropertyLabels.Business, PhoneLabels.Fax] = phoneNumber.ToPhoneNumber();
            }
        }

        public string HomePhone
        {
            get
            {
                return base.Source.PhoneNumbers[PropertyLabels.Personal, PhoneLabels.Voice].Number;
            }
            set
            {
                PhoneNumberBuilder phoneNumber = new PhoneNumberBuilder(base.Source.PhoneNumbers[PropertyLabels.Personal, PhoneLabels.Voice]);
                phoneNumber.Number = value;
                base.Source.PhoneNumbers[PropertyLabels.Personal, PhoneLabels.Voice] = phoneNumber.ToPhoneNumber();
            }
        }

        public string PersonalMobile
        {
            get
            {
                return base.Source.PhoneNumbers[PropertyLabels.Personal, PhoneLabels.Mobile].Number;
            }
            set
            {
                PhoneNumberBuilder phoneNumber = new PhoneNumberBuilder(base.Source.PhoneNumbers[PropertyLabels.Personal, PhoneLabels.Mobile]);
                phoneNumber.Number = value;
                base.Source.PhoneNumbers[PropertyLabels.Personal, PhoneLabels.Mobile] = phoneNumber.ToPhoneNumber();
            }
        }

        public string HomeFax
        {
            get
            {
                return base.Source.PhoneNumbers[PropertyLabels.Personal, PhoneLabels.Fax].Number;
            }
            set
            {
                PhoneNumberBuilder phoneNumber = new PhoneNumberBuilder(base.Source.PhoneNumbers[PropertyLabels.Personal, PhoneLabels.Fax]);
                phoneNumber.Number = value;
                base.Source.PhoneNumbers[PropertyLabels.Personal, PhoneLabels.Fax] = phoneNumber.ToPhoneNumber();
            }
        }

        #endregion Phone Properties
                      
        /// <summary>
        /// The 1st (primary) email address for the contact
        /// </summary>        
        public string PersonalEmail1
        {
            get
            {
                return base.Source.EmailAddresses.Default.Address;
            }
            set
            {

                EmailAddressBuilder email = new EmailAddressBuilder(base.Source.EmailAddresses.Default);
                email.Address = value;
                base.Source.EmailAddresses.Default = email.ToEmailAddress();
            }
        }
        
        public string PersonalEmail1Type
        {
            get
            {
                return base.Source.EmailAddresses.Default.Type;
            }
            set
            {
                EmailAddressBuilder email = new EmailAddressBuilder(base.Source.EmailAddresses.Default);
                email.Type = value;
                base.Source.EmailAddresses.Default = email.ToEmailAddress();
            }
        }

        /// <summary>
        /// The 2nd email address for the contact
        /// </summary>        
        public string PersonalEmail2
        {
            get
            {
                return base.Source.EmailAddresses[PropertyLabels.Personal].Address;
            }
            set
            {
                EmailAddressBuilder email = new EmailAddressBuilder(base.Source.EmailAddresses[PropertyLabels.Personal]);
                email.Address = value;
                base.Source.EmailAddresses[PropertyLabels.Personal] = email.ToEmailAddress();
            }
        }

        public string PeronalEmail2Type
        {
            get
            {
                return base.Source.EmailAddresses[PropertyLabels.Personal].Type;
            }
            set
            {
                EmailAddressBuilder email = new EmailAddressBuilder(base.Source.EmailAddresses[PropertyLabels.Personal]);
                email.Type = value;
                base.Source.EmailAddresses[PropertyLabels.Personal] = email.ToEmailAddress();
            }
        }

        /// <summary>
        /// The business email address for the contact
        /// </summary>
        public string BusinessEmail
        {
            get
            {
                return base.Source.EmailAddresses[PropertyLabels.Business].Address;
            }
            set
            {
                EmailAddressBuilder email = new EmailAddressBuilder(base.Source.EmailAddresses[PropertyLabels.Business]);
                email.Address = value;
                base.Source.EmailAddresses[PropertyLabels.Business] = email.ToEmailAddress();
            }
        }

        public string BusinessEmailType
        {
            get
            {
                return base.Source.EmailAddresses[PropertyLabels.Business].Type;
            }
            set
            {
                EmailAddressBuilder email = new EmailAddressBuilder(base.Source.EmailAddresses[PropertyLabels.Business]);
                email.Type = value;
                base.Source.EmailAddresses[PropertyLabels.Business] = email.ToEmailAddress();
            }
        }

        /// <summary>
        /// The organization associated with the contact
        /// </summary>
        public string Organization
        {
            get
            {
                return base.Source.Positions[PropertyLabels.Business].Organization;
            }
            set
            {
                PositionBuilder position = new PositionBuilder(base.Source.Positions[PropertyLabels.Business]);
                position.Organization = value;
                base.Source.Positions[PropertyLabels.Business] = position.ToPosition();
            }
        }

        /// <summary>
        /// The role associated with the contact
        /// </summary>
        public string Role
        {
            get
            {
                return base.Source.Positions[PropertyLabels.Business].Role;
            }
            set
            {                
                PositionBuilder position = new PositionBuilder(base.Source.Positions[PropertyLabels.Business]);
                position.Role = value;
                base.Source.Positions[PropertyLabels.Business] = position.ToPosition();
            }
        }

        /// <summary>
        /// The company associated with the contact.
        /// </summary>
        public string Company
        {
            get
            {
                return base.Source.Positions[PropertyLabels.Business].Company;
            }
            set
            {                
                PositionBuilder position = new PositionBuilder(base.Source.Positions[PropertyLabels.Business]);
                position.Company = value;
                base.Source.Positions[PropertyLabels.Business] = position.ToPosition();
            }
        }

        /// <summary>
        /// The department associated with the contact
        /// </summary>
        public string Department
        {
            get
            {
                return base.Source.Positions[PropertyLabels.Business].Department;
            }
            set
            {
                PositionBuilder position = new PositionBuilder(base.Source.Positions[PropertyLabels.Business]);
                position.Department = value;
                base.Source.Positions[PropertyLabels.Business] = position.ToPosition();
            }
        }

        /// <summary>
        /// The office associated with the contact
        /// </summary>
        public string Office
        {
            get
            {
                return base.Source.Positions[PropertyLabels.Business].Office;
            }
            set
            {
                PositionBuilder position = new PositionBuilder(base.Source.Positions[PropertyLabels.Business]);
                position.Office = value;
                base.Source.Positions[PropertyLabels.Business] = position.ToPosition();
            }
        }

        /// <summary>
        /// The job title of the contact
        /// </summary>
        public string JobTitle
        {
            get
            {
                return base.Source.Positions[PropertyLabels.Business].JobTitle;
            }
            set
            {
                PositionBuilder position = new PositionBuilder(base.Source.Positions[PropertyLabels.Business]);
                position.JobTitle = value;
                base.Source.Positions[PropertyLabels.Business] = position.ToPosition();
            }
        }

        /// <summary>
        /// The profession of the contact
        /// </summary>
        public string Profession
        {
            get
            {
                return base.Source.Positions[PropertyLabels.Business].Profession;
            }
            set
            {
                PositionBuilder position = new PositionBuilder(base.Source.Positions[PropertyLabels.Business]);
                position.Profession = value;
                base.Source.Positions[PropertyLabels.Business] = position.ToPosition();
            }
        }

        /// <summary>
        /// The 1st (primary) im address for the contact
        /// </summary>
        public string IMAddress1
        {
            get
            {
                return base.Source.IMAddresses.Default.Handle;
            }
            set
            {
                IMAddressBuilder im = new IMAddressBuilder(base.Source.IMAddresses.Default);
                im.Handle = value;
                base.Source.IMAddresses.Default = im.ToIMAddress();
            }
        }

        /// <summary>
        /// The 2nd im address for the contact
        /// </summary>
        public string IMAddress2
        {
            get
            {
                return base.Source.IMAddresses[PropertyLabels.Personal].Handle;
            }
            set
            {
                IMAddressBuilder im = new IMAddressBuilder(base.Source.IMAddresses[PropertyLabels.Personal]);
                im.Handle = value;
                base.Source.IMAddresses[PropertyLabels.Personal] = im.ToIMAddress();
            }
        }

        /// <summary>
        /// Any object to be stored with the contact. This object is not persisted to the contact source.
        /// </summary>
        public object Tag { get; set; }

        #endregion Properties

        #region Methods

        #region Private

        private void UpdateFormattedName()
        {
            NameBuilder name = new NameBuilder(base.Source.Names.Default);
            name.FormattedName = Name.FormatName(this.FirstName, this.MiddleName, this.LastName, NameCatenationOrder.GivenMiddleFamily);
            base.Source.Names.Default = name.ToName();
        }

        #endregion Private

        #region Public

        /// <summary>
        /// Gets the associated Contact object
        /// </summary>
        /// <returns>A Contact object</returns>
        public Contact GetContact()
        {
            return base.Source;
        }

        /// <summary>
        /// Saves the contact
        /// </summary>
        public void CommitChanges()
        {
            base.Source.CommitChanges();
        }

        /// <summary>
        /// Saves the contact to a vCard at the specified file path
        /// </summary>
        /// <param name="filePath">The location and file name of the vCard to be saved.</param>
        public void SaveToVCard21(string filePath)
        {
            Contact.SaveToVCard21(base.Source, filePath);
        }

        /// <summary>
        /// Creates a SimpleContactView collection from a dataset.
        /// </summary>
        /// <param name="contacts">The dataset to create the collection from.</param>
        /// <returns>A collection of SimpleContactView objects</returns>
        public static ICollection<SimpleContactView> CreateFromDataset(SimpleContactDataSet contacts)
        {
            SimpleContactView simpleContact = null;
            ICollection<SimpleContactView> simpleContacts = new List<SimpleContactView>();

            ContactManager mgr = new ContactManager();

            foreach (SimpleContactDataSet.SimpleContactRow row in contacts.SimpleContact)
            {                
                simpleContact = new SimpleContactView(mgr.CreateContact(Enum.IsDefined(typeof(ContactType), row.ContactType) ? (ContactType)Enum.Parse(typeof(ContactType), row.ContactType) : ContactType.Unknown));
                
                simpleContact.Notes = row.Notes;
                simpleContact.Gender = Enum.IsDefined(typeof(Gender), row.Gender) ? (Gender)Enum.Parse(typeof(Gender), row.Gender) : Gender.Unspecified;

                if (!row.IsPicNull())
                {
                    using (MemoryStream mem = new MemoryStream(row.Pic))
                    {
                        simpleContact.Source.Photos[PhotoLabels.UserTile] = new PhotoBuilder(mem, "image").ToPhoto();
                    }
                }

                simpleContact.IMAddress1 = row.IMAddress1;
                simpleContact.IMAddress2 = row.IMAddress2;

                #region Name Properties
                simpleContact.FirstName = row.FirstName;
                simpleContact.LastName = row.LastName;
                simpleContact.MiddleName = row.MiddleName;
                simpleContact.NickName = row.Nickname;
                simpleContact.Suffix = row.Suffix;
                simpleContact.Prefix = row.Prefix;
                simpleContact.Title = row.Title;
                #endregion Name Properties

                #region Address Properties
                simpleContact.HomeStreet = row.HomeStreet;
                simpleContact.HomeCity = row.HomeCity;
                simpleContact.HomeState = row.HomeState;
                simpleContact.HomeCountry = row.HomeCountry;
                simpleContact.HomeZip = row.HomeZip;
                simpleContact.BusinessStreet = row.BusinessStreet;
                simpleContact.BusinessCity = row.BusinessCity;
                simpleContact.BusinessState = row.BusinessState;
                simpleContact.BusinessCountry = row.BusinessCountry;
                simpleContact.BusinessZip = row.BusinessZip;
                #endregion Address Properties

                #region Phone Properties
                simpleContact.BusinessPhone = row.BusinessPhone;
                simpleContact.BusinessMobile = row.BusinessMobile;
                simpleContact.BusinessFax = row.BusinessFax;
                simpleContact.HomePhone = row.HomePhone;
                simpleContact.PersonalMobile = row.PersonalMobile;
                simpleContact.HomeFax = row.HomeFax;
                #endregion Phone Properties

                #region Email Properties
                simpleContact.PersonalEmail1 = row.PersonalEmail1;
                simpleContact.PersonalEmail1Type = row.PersonalEmail1Type;
                simpleContact.PersonalEmail2 = row.PersonalEmail2;
                simpleContact.PeronalEmail2Type = row.PersonalEmail2Type;
                simpleContact.BusinessEmail = row.BusinessEmail;
                simpleContact.BusinessEmailType = row.BusinessEmailType;
                #endregion Email Properties

                #region Position Properties
                simpleContact.Organization = row.Organization;
                simpleContact.Role = row.Role;
                simpleContact.Company = row.Company;
                simpleContact.Department = row.Department;
                simpleContact.Office = row.Office;
                simpleContact.JobTitle = row.JobTitle;
                simpleContact.Profession = row.Profession;
                #endregion Position Properties

                simpleContacts.Add(simpleContact);
            }

            return simpleContacts;
        }

        /// <summary>
        /// Converts the current contact to a databaset.
        /// </summary>
        /// <returns>A SimpleContactDataSet object.</returns>
        public SimpleContactDataSet ToDataset()
        {
            SimpleContactDataSet ds = new SimpleContactDataSet();
            SimpleContactDataSet.SimpleContactRow row = (SimpleContactDataSet.SimpleContactRow)ds.SimpleContact.NewRow();

            row.ContactID = Guid.NewGuid();
            row.ContactType = this.ContactType.ToString();
            row.Notes = this.Notes;
            row.Gender = this.Gender.ToString();

            if (this.PicAsImage != null)
            {
                using (MemoryStream mem = new MemoryStream())
                {
                    this.PicAsImage.Save(mem, this.PicAsImage.RawFormat);
                    row.Pic = mem.ToArray();
                }
            }
            else if (this.PicAsBitmapSource != null)
            {
                using (MemoryStream mem = new MemoryStream())
                {
                    this.PicAsImage.Save(mem, this.PicAsImage.RawFormat);
                    row.Pic = mem.ToArray();
                }
            }

            row.IMAddress1 = this.IMAddress1;
            row.IMAddress2 = this.IMAddress2;

            #region Name Properties
            row.FirstName = this.FirstName;
            row.LastName = this.LastName;
            row.MiddleName = this.MiddleName;
            row.Nickname = this.NickName;
            row.Suffix = this.Suffix;
            row.Prefix = this.Prefix;
            row.Title = this.Title;
            #endregion Name Properties

            #region Address Properties
            row.HomeStreet = this.HomeStreet;
            row.HomeCity = this.HomeCity;
            row.HomeState = this.HomeState;
            row.HomeCountry = this.HomeCountry;
            row.HomeZip = this.HomeZip;
            row.BusinessStreet = this.BusinessStreet;
            row.BusinessCity = this.BusinessCity;
            row.BusinessState = this.BusinessState;
            row.BusinessCountry = this.BusinessCountry;
            row.BusinessZip = this.BusinessZip;
            #endregion Address Properties

            #region Phone Properties
            row.BusinessPhone = this.BusinessPhone;
            row.BusinessMobile = this.BusinessMobile;
            row.BusinessFax = this.BusinessFax;
            row.HomePhone = this.HomePhone;
            row.PersonalMobile = this.PersonalMobile;
            row.HomeFax = this.HomeFax;
            #endregion Phone Properties

            #region Email Properties
            row.PersonalEmail1 = this.PersonalEmail1;
            row.PersonalEmail1Type = this.PersonalEmail1Type;
            row.PersonalEmail2 = this.PersonalEmail2;
            row.PersonalEmail2Type = this.PeronalEmail2Type;
            row.BusinessEmail = this.BusinessEmail;
            row.BusinessEmailType = this.BusinessEmailType;
            #endregion Email Properties

            #region Position Properties
            row.Organization = this.Organization;
            row.Role = this.Role;
            row.Company = this.Company;
            row.Department = this.Department;
            row.Office = this.Office;
            row.JobTitle = this.JobTitle;
            row.Profession = this.Profession;
            #endregion Position Properties

            ds.SimpleContact.AddSimpleContactRow(row);

            return ds;
        }

        #endregion Public 

        #endregion Methods
        
    }
}
